package com.biao.service;

import com.biao.annotation.MyTransactional;
import com.biao.entity.User;
import com.biao.template.MyJdbcTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author: chenwenbiao
 * @create: 2020-2-2 12:41
 */
@Service
public class UserService {

    @Autowired
    private MyJdbcTemplate myJdbcTemplate;

    @MyTransactional    // 要throws异常才能回滚，如果被try catch那就相当于没有异常，所以不会回滚
    public void addUser(User user) throws Exception {
        String sql = "insert into t_user(age, name, address) " +
                "values(" + user.getAge() + ",'" + user.getName() + "','" + user.getAddress() + "')";

        System.out.println(sql);
        myJdbcTemplate.execute(sql);

        String sql2 = "insert into t_log(content)values('增加用户,用户名为："+user.getName()+"')";
        myJdbcTemplate.execute(sql2);
        System.out.println("==========Add user completed============");

        //测试回滚功能
        System.out.println("制造异常，测试回滚功能!");
        throw new Exception();
    }

}
